package com.derbysoft.lowcostadapter.web.action.admin.config;

import com.derbysoft.common.web.struts2.support.PaginateActionSupport;
import com.derbysoft.lowcostadapter.commons.model.Channel;
import com.derbysoft.lowcostadapter.commons.model.ProviderIdentification;
import com.derbysoft.lowcostadapter.commons.model.SystemVariable;
import com.derbysoft.lowcostadapter.commons.redis.service.ChannelCacheService;
import com.derbysoft.lowcostadapter.commons.redis.service.ProviderIdentificationCacheService;
import com.derbysoft.lowcostadapter.commons.redis.service.SystemVariableCacheService;
import com.derbysoft.lowcostadapter.commons.repository.ChannelRepository;
import com.derbysoft.lowcostadapter.commons.repository.ProviderIdentificationRepository;
import com.derbysoft.lowcostadapter.commons.repository.SystemVariableRepository;
import com.derbysoft.lowcostadapter.dswitch2.service.helper.Constant;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class ListAction extends PaginateActionSupport {

    @Autowired
    private SystemVariableRepository systemVariableRepository;

    @Autowired
    private SystemVariableCacheService systemVariableCacheService;

    @Autowired
    private ChannelRepository channelRepository;

    @Autowired
    private ProviderIdentificationRepository identificationRepository;

    @Autowired
    private ProviderIdentificationCacheService providerIdentificationCacheService;

    @Autowired
    private ChannelCacheService channelCacheService;

    private SystemVariable appendDerbyLog;
    private SystemVariable appendFailedAvailLogOnly;
    private SystemVariable localFolder;
    private SystemVariable occupancyCheckStrict;

    private String channels;
    private String identifications;

    @Action("list")
    public String list() {
        appendDerbyLog = systemVariableRepository.findByName(Constant.APPEND_DERBY_LOG);
        appendFailedAvailLogOnly = systemVariableRepository.findByName(Constant.APPEND_FAILED_AVAIL_LOG_ONLY);
        localFolder = systemVariableRepository.findByName(Constant.LOCAL_FOLDER);
        occupancyCheckStrict = systemVariableRepository.findByName(Constant.OCCUPANCY_CHECK_STRICT);

        List<Channel> channelList = channelRepository.loadAll();
        StringBuilder channelBuilder = new StringBuilder();
        for (Channel channel : channelList) {
            channelBuilder.append(String.format("Channel: [%s], Availability Provider:[%s],Normal Make Reservation: [%b]",
                    channel.getChannelCode(), channel.getAvailabilityProvider(), channel.getNormalMakeReservation())).append("<br>");
        }
        channels = channelBuilder.toString();

        List<ProviderIdentification> identificationList = identificationRepository.loadAll();
        StringBuilder builder = new StringBuilder();
        for (ProviderIdentification identification : identificationList) {
            builder.append(String.format("Channel: [%s], UserName: [%s]", identification.getChannelCode(), identification.getUserName())).append("<br>");
        }
        identifications = builder.toString();
        return SUCCESS;
    }

    @Action("update")
    public String update() {
        if (appendDerbyLog.getValue() == null) {
            appendDerbyLog.setValue("false");
        }
        if (appendFailedAvailLogOnly.getValue() == null) {
            appendFailedAvailLogOnly.setValue("false");
        }
        if (occupancyCheckStrict.getValue() == null) {
            occupancyCheckStrict.setValue("false");
        }
        systemVariableRepository.update(appendDerbyLog.getName(), appendDerbyLog.getValue());
        systemVariableRepository.update(appendFailedAvailLogOnly.getName(), appendFailedAvailLogOnly.getValue());
        systemVariableRepository.update(localFolder.getName(), localFolder.getValue());
        systemVariableRepository.update(occupancyCheckStrict.getName(), occupancyCheckStrict.getValue());
        refreshToCache();
        return "list";
    }

    private void refreshToCache() {
        List<SystemVariable> systemVariables = new ArrayList<SystemVariable>();
        systemVariables.add(appendDerbyLog);
        systemVariables.add(appendFailedAvailLogOnly);
        systemVariables.add(localFolder);
        systemVariables.add(occupancyCheckStrict);
        systemVariableCacheService.saveAll(systemVariables);

        channelCacheService.saveAll(channelRepository.loadAll());
        providerIdentificationCacheService.saveAll(identificationRepository.loadAll());
    }


    public SystemVariable getOccupancyCheckStrict() {
        return occupancyCheckStrict;
    }

    public void setOccupancyCheckStrict(SystemVariable occupancyCheckStrict) {
        this.occupancyCheckStrict = occupancyCheckStrict;
    }

    public SystemVariable getAppendDerbyLog() {
        return appendDerbyLog;
    }

    public void setAppendDerbyLog(SystemVariable appendDerbyLog) {
        this.appendDerbyLog = appendDerbyLog;
    }

    public SystemVariable getAppendFailedAvailLogOnly() {
        return appendFailedAvailLogOnly;
    }

    public void setAppendFailedAvailLogOnly(SystemVariable appendFailedAvailLogOnly) {
        this.appendFailedAvailLogOnly = appendFailedAvailLogOnly;
    }

    public SystemVariable getLocalFolder() {
        return localFolder;
    }

    public void setLocalFolder(SystemVariable localFolder) {
        this.localFolder = localFolder;
    }

    public String getChannels() {
        return channels;
    }

    public String getIdentifications() {
        return identifications;
    }
}
